<template>
  <div class="goods">
    <div class="wrap">
      <Crumb :data="nav"> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span :class="{ active: index == active1 }" @click="options1Fn(index, item.min, item.max)"
            v-for="(item, index) in options1  " :key="index">{{ item.txt
            }}</span>

        </li>
        <li>
          <strong>分类：</strong>
          <span :class="{ active: item.inShow }" v-for="(item, index) in options2" :key="index"
            @click="options2Fn(index, item.type)">{{
                item.txt
            }}</span>

        </li>
      </ul>
      <List :arr="arrListShow" />
      <div class="load">
        {{ isReachBottom ? "加载完毕 !" : "加载中" }}
        <div class="wrapper" v-show="!isReachBottom">
          <div class="circle"></div>
          <div class="circle"></div>
          <div class="circle"></div>
          <div class="shadow"></div>
          <div class="shadow"></div>
          <div class="shadow"></div>
        </div>
        <div class="planet">
        </div>
      </div>
    </div>
  </div>
</template>                            
<script>

import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
import { ProductsAPI } from "@/request/api";
import { getScrollTop, getClientHeight, getScrollHeight } from "@/Utils/index"
export default {
  data() {
    return {
      nav: [
        { name: "首页", link: "#" },
        { name: "全部商品", link: "#" },
      ],
      // 栏目
      did: 0,
      // 类型
      type: 0,
      // 最小积分,
      min: 0,
      // 最大积分
      max: 0,
      // 关键字
      keyword: "",
      // 商品
      arrList: [],
      // 默认显示商品
      arrListShow: [],
      // 排序的数组项
      options1: [
        { min: 0, max: 10000, txt: "全部" },
        // "我还可以兑换"的max值是Header组件的coin值
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500分" },
        { min: 500, max: 1000, txt: "500-1000分" },
        { min: 1000, max: 1500, txt: "1000-1500分" },
        { min: 1500, max: 2500, txt: "1500-2500分" },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      // 
      options2: [
        { txt: "全部商品", type: 0, inShow: true },
        { txt: "事物礼品", type: 1, inShow: false },
        { txt: "虚拟礼品", type: 2, inShow: false }
      ],
      // 高亮
      active1: 0,
      // 当前页
      page: 1,
      // 当前页条数
      size: 8,
      // 是否在加载数据
      isLoading: false,
      //    
      isReachBottom: false
    };
  },
  components: {
    Crumb,
    List,
  },
  created() {
    // 搜索关键字keyword
    this.keyword = this.$route.query.keyword
    this.Products()


  },
  mounted() {
    // 监听滚动事件
    window.addEventListener("scroll", this.scrollFn)
  },
  beforeDestroy() {
    window.removeEventListener("scroll", this.scrollFn)
  },
  // 全部商品请求
  methods: {
    async Products() {
      let res = await ProductsAPI({
        did: this.did,
        type: this.type,
        min: this.min,
        max: this.max,
        keyword: this.keyword,
      })
      this.arrList = res.data.data
      // 默认显示商品
      this.arrListShow = this.arrList.filter((item, index) => index < 8)
      // 初始化
      this.page = 1
      this.isReachBottom = false
      if (this.arrList.length == this.arrListShow.length) {
        this.isReachBottom = true;
        return
      }
      // console.log(this.arrList);
    },
    options1Fn(index, min, max) {
      this.active1 = index
      //更新查询条件
      this.min = min
      this.max = max
      // console.log(min, max);
      this.keyword = ""
      //重新查询
      this.Products()
    },
    options2Fn(index, type) {
      //排他
      //让所有属性都为false
      this.options2.forEach(item => {
        item.inShow = false
      })
      //  让当前自己为true
      this.options2[index].inShow = true
      // console.log(tyoe);
      this.type = type
      this.Products()
    },
    // 滚动事件的函数
    scrollFn() {

      if (this.arrList.length == this.arrListShow.length) {
        this.isReachBottom = true;
        return
      }

      // 滚动条当前的位置 
      // getScrollTop()
      // // 获取当前可视范围的高度 
      // getClientHeight()
      // // 文档完整的高度 
      // getScrollHeight()
      if (getScrollTop() + getClientHeight() > getScrollHeight() - 300) {

        if (this.isLoading == false) {
          this.isLoading = true
          this.page++
          // 方式二:从arrList数组中拿第二页的数据,把数据从arrList push到arrListShow
          // page:1 size:8 index 0-7 数据
          // page:2 size:8 index 8-15 数据
          // page:3 size:8 index 16-23 数据
          setTimeout(() => {
            for (var i = (this.page - 1) * this.size; i < this.page * this.size; i++) {
              this.arrList[i] && this.arrListShow.push(this.arrList[i])
              console.log("for", this.page, this.arrList[i]);
            }
            this.isLoading = false
          }, 3000)
        }

      }
      // console.log("滚动了", getScrollTop(), getClientHeight(), getScrollHeight());

    }
  },
  watch: {
    "$route.query.keyword": {
      handler(n, o) {
        // console.log(n, o);
        this.keyword = n
        this.Products()
      }
    }
  }
};
</script>
 
<style lang = "less" scoped>
@import "../assets/css/public.less";

.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;

  .options {
    padding-top: 20px;
    padding-bottom: 40px;

    li {
      margin-top: 20px;

      strong {
        color: #000;
        font-weight: bold;
      }

      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;

        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
}

.load {
  text-align: center;
}

/* From uiverse.io by @fanishah */
.planet {
  float: right;
  margin: 10px auto;
  display: block;
  width: 125px;
  height: 125px;
  position: relative;
  transform-style: preserve-3d;
  border-radius: 50%;
  background: #fcc96b;
  background: #fcc96b;
  background: linear-gradient(180deg,
      #fcc96b 0%,
      #fcc96b 15%,
      #f7ae01 15%,
      #f7ae01 19%,
      #fcc96b 19%,
      #fcc96b 22%,
      #f7ae01 22%,
      #f7ae01 28%,
      #fcc96b 28%,
      #fcc96b 31%,
      #fcc96b 33%,
      #fcc96b 36%,
      #f7ae01 36%,
      #f7ae01 48%,
      #fcc96b 48%,
      #fcc96b 55%,
      #f7ae01 55%,
      #f7ae01 66%,
      #fcc96b 66%,
      #fcc96b 70%,
      #f7ae01 70%,
      #f7ae01 73%,
      #fcc96b 73%,
      #fcc96b 82%,
      #f7ae01 82%,
      #c7ba9d 86%,
      #fcc96b 86%);
  box-shadow: inset 0 0 25px rgba(0, 0, 0, 0.25),
    inset 8px -4px 6px rgba(199, 128, 0, 0.5),
    inset -8px 4px 8px rgba(255, 235, 199, 0.5), inset 20px -5px 12px #f7ae01,
    0 0 100px #ffffff59;
  transform: rotateZ(-15deg);
}

.planet::before {
  position: absolute;
  content: "";
  display: block;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  border: 16px solid #e1a519;
  border-top-width: 0;
  border-radius: 50%;
  box-shadow: 0 -2px 0 #c18620;
  animation: rings1 0.8s infinite linear;
}

.planet::after {
  position: absolute;
  content: "";
  display: block;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  border: 8px solid #d48b0c;
  border-top-width: 0;
  border-radius: 50%;
  box-shadow: 0 -2px 0 #b99309;
  animation: rings2 0.8s infinite linear;
}

@keyframes rings1 {
  0% {
    transform: rotateX(65deg) rotateZ(0deg) scale(1.75);
  }

  100% {
    transform: rotateX(65deg) rotateZ(360deg) scale(1.75);
  }
}

@keyframes rings2 {
  0% {
    transform: rotateX(65deg) rotateZ(0deg) scale(1.7);
  }

  100% {
    transform: rotateX(65deg) rotateZ(360deg) scale(1.7);
  }
}

/* From uiverse.io by @mobinkakei */
.wrapper {
  display: inline-block;
  width: 200px;
  height: 60px;
  position: relative;
  z-index: 1;
}

.circle {
  width: 20px;
  height: 20px;
  position: absolute;
  border-radius: 50%;
  background-color: #fff;
  left: 15%;
  transform-origin: 50%;
  animation: circle7124 .5s alternate infinite ease;
}

@keyframes circle7124 {
  0% {
    top: 60px;
    height: 5px;
    border-radius: 50px 50px 25px 25px;
    transform: scaleX(1.7);
  }

  40% {
    height: 20px;
    border-radius: 50%;
    transform: scaleX(1);
  }

  100% {
    top: 0%;
  }
}

.circle:nth-child(2) {
  left: 45%;
  animation-delay: .2s;
}

.circle:nth-child(3) {
  left: auto;
  right: 15%;
  animation-delay: .3s;
}

.shadow {
  width: 20px;
  height: 4px;
  border-radius: 50%;
  background-color: rgba(0, 0, 0, 0.9);
  position: absolute;
  top: 62px;
  transform-origin: 50%;
  z-index: -1;
  left: 15%;
  filter: blur(1px);
  animation: shadow046 .5s alternate infinite ease;
}

@keyframes shadow046 {
  0% {
    transform: scaleX(1.5);
  }

  40% {
    transform: scaleX(1);
    opacity: .7;
  }

  100% {
    transform: scaleX(.2);
    opacity: .4;
  }
}

.shadow:nth-child(4) {
  left: 45%;
  animation-delay: .2s
}

.shadow:nth-child(5) {
  left: auto;
  right: 15%;
  animation-delay: .3s;
}
</style>